<?php
/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2017/9/9
 * Time: 上午9:46
 */
defined('BASEPATH') OR exit('No direct script access allowed');
class Message extends CI_Model
{

    public function __construct() {
        $this->load->database();
        $this->load->library('session');
        $this->load->helper("url");
    }


    function send_msg($tel,$msg,$minute=10,$count=3,$session_minute='',$needcheck=false){
//        if($needcheck)
//        {
//            if(!$this->check_send_msg($tel,$minute,$count,$session_minute))
//            {
//                return array('status'=>'fail','msg'=>'短信'.$minute.'分钟只能发送'.$count.'次');
//            }
//        }

        if ($msg && $tel){
            $url = "http://sms.10690221.com:9011/hy/";

            $auth = md5('yhwj'.'Gen2018wjyh');
            $post_string = "uid=503831&auth=".$auth."&mobile=".$tel."&msg=".urlencode($msg)."&expid=0&encode=utf-8";

            $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $data = curl_exec($ch);
            curl_close($ch);
            $arr = explode(',',$data);
            if($arr[0]==='0') {
//                $this->update_send_msg($tel);
                return array('code'=>0,'msg'=>'短信发送成功');
            }else{
                log_message('error', "短信发送失败,手机".$tel."内容".$msg.",代码".$arr[0]);
                return array('code'=>-1,'msg'=>'短信发送失败');
            }
        }else{
            return array('code'=>-1,'msg'=>'参数错误，短信发送失败');
        }


    }

    // 存入session
    public function add_msg_session($tel,$code)
    {
        $data1['time'] = date("Y-m-d H:i:s");
        $data1['code'] = $code;
        $_SESSION[$tel."code"] = json_encode($data1);
    }

    // 校验session中内容
    public function check_msg_session($tel,$code)
    {
        $over_time = $this->config->item("over_time");
        $rs = isset($_SESSION[$tel."code"]) ? $_SESSION[$tel."code"] : '';
        if($rs){
            $data = json_decode($rs,true);
            $minute=floor((time()-strtotime($data['time']))%86400/60);
            if($minute>$over_time){
                return ["code"=>-1,"msg"=>"此验证码已超过15分钟,已失效"];
            }
            if($data['code'] == $code){
                return ["code"=>0,"msg"=>"success"];
            }else{
                return ["code"=>-1,"msg"=>"验证码错误"];
            }
        }else{
            return ["code"=>-1,"msg"=>"此手机没有发送验证码"];
        }
    }

    // 校验是否可以发送短信 cj
    function check_send_msg($tel,$minute=10,$count=3,$session_minute=''){
        if($session_minute){

        }
        $time = date('Y-m-d H:i:s',strtotime("-".$minute." minute"));
        $sql = " select count(id) as countres from send_record where tel = '".$tel."' and time >= '".$time . "'";
        $query = $this->db->query($sql);
        $res = $query->result_array();
        if($res[0]['countres'] >= $count)
        {
            return false;
        }else{
            return true;
        }
    }

    // 更新短信发送记录 cj
    function update_send_msg($tel,$minute=10,$count=3,$session_minute=''){
        $time = date('Y-m-d H:i:s');
        $sql = "INSERT INTO send_record SET tel = '".$tel."', time = '".$time."'";
        return $this->db->query($sql);
    }

}